﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Blogs01
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            var cmds = "select * from Articles";
            var pp = DbHelper.GetDataTable(cmds);
            dataGridView1.DataSource = pp;
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dataGridView1.AllowUserToAddRows = false;

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {

        }

        //查找
        private void button1_Click(object sender, EventArgs e)
        {
            var Title = textBox1.Text;
            var sql = string.Format("select * from Articles  where  Title like '%{0}%'", Title);
            var ss = DbHelper.GetDataTable(sql);
            dataGridView1.DataSource = ss;
        }

        //添加
        private void button2_Click(object sender, EventArgs e)
        {
            Form3 form3 = new Form3();
            var res = form3.ShowDialog();
            if (res == DialogResult.Yes)
            {
                var Title = textBox1.Text;
                var cmdd = string.Format("select * from Articles where Title like '%{0}%'", Title);
                var dt = DbHelper.GetDataTable(cmdd);
                dataGridView1.DataSource = dt;
            }
        }

        //删除
        private void button3_Click_1(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                var shan = dataGridView1.SelectedRows[0];
                var id = (int)shan.Cells["Id"].Value;
                var qus = MessageBox.Show("请确定删除", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
                if (qus.Equals(DialogResult.Yes))
                {
                    var sql = string.Format("delete from Articles where id = {0}", id);
                    var coon = DbHelper.Addddd(sql);
                    if (coon > 0)
                    {
                        var Title = textBox1.Text;
                        var cmd = string.Format("select * from Articles where Title like '%{0}%'", Title);
                        var dt = DbHelper.GetDataTable(cmd);
                        dataGridView1.DataSource = dt;

                        MessageBox.Show("删除成功！");
                    }
                }
            }
        }



        //更新
        private void button6_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                var Id = (int)dataGridView1.SelectedRows[0].Cells["Id"].Value;
                var Title = (string)dataGridView1.SelectedRows[0].Cells["Title"].Value;
                var Author = (string)dataGridView1.SelectedRows[0].Cells["Author"].Value;
                var CreatedTime = (string)dataGridView1.SelectedRows[0].Cells["CreatedTime"].Value;


                Form4 form4 = new Form4(Id,Title,Author,CreatedTime);
                var geng = form4.ShowDialog();

                if (geng == DialogResult.Yes)
                {
                    var cmd = string.Format("select * from Articles", Title);
                    var oo = DbHelper.GetDataTable(cmd);
                    dataGridView1.DataSource = oo;
                }
            }
            else
            {
                MessageBox.Show("请选择内容");

            }
        }
    }
}
